﻿using System.Data.Entity;
using Transport.Core.Models.Users;
using Transport.Data.Mapping.Users;
using Transport.Core.Models.ContentManagement;
using Transport.Core.Models.Settings;
using Transport.Core.Models.Parcels;
using Transport.Core.Models.Customers;

namespace Transport.Data
{
    public class TransportContext : DbContext
    {
        //DbSets
        public DbSet<Role> Roles { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<NewsCategoryItem> NewsCategoryItems { get; set; }
        public DbSet<NewsItem> NewsItems { get; set; }
        public DbSet<StaticPage> StaticPages { get; set; }
        public DbSet<MyOffice> MyOffices { get; set; }
        public DbSet<PostOffice> PostOffices { get; set; }
        public DbSet<RateMapping> RateMappings { get; set; }
        public DbSet<RatesTable> RatesTables { get; set; }
        public DbSet<Region> Regions { get; set; }
        public DbSet<StateProvince> StateProvinces { get; set; }
        public DbSet<Customer> Customers { get; set; }
        public DbSet<CustomerLogo> CustomerLogos { get; set; }
        public DbSet<Parcel> Parcels { get; set; }
        public DbSet<Tracking> Trackings { get; set; }
        public DbSet<ParcelDeliveryGroup> ParcelDeliveryGroups { get; set; }
        public DbSet<ParcelDelivery> ParcelDeliverys { get; set; }
        public DbSet<StateProvinceBelongToRegion> StateProvinceBelongToRegions { get; set; }
        public DbSet<ParcelPackagingGroup> ParcelPackagingGroups { get; set; }
        public DbSet<ParcelPackagingItem> ParcelPackagingItems { get; set; }

        private static object _objLock = new object();
        private static TransportContext _current = null;

        /// <summary>
        /// DbContext singleton pattern
        /// </summary>
        public static TransportContext Current
        {
            get
            {
                lock (_objLock)
                {
                    if (_current == null)
                    {
                        _current = new TransportContext();
                    }
                    return _current;
                }
            }
        }

        /// <summary>
        /// Contructor
        /// </summary>
        public TransportContext()
        {
            var config = System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"];
            if (config != null)
            {
                this.Database.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
            }
        }

        /// <summary>
        /// Mapping data
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new UserMap());
            base.OnModelCreating(modelBuilder);
        }
    }
}
